package component;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.components.ApplicationComponent;
import org.jetbrains.annotations.NotNull;
import utils.GameUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther syc.
 * @Date 2016/11/14 11:34.
 */
public class RunCmdComponent implements ApplicationComponent {
    public RunCmdComponent() {
    }

    @Override
    public void initComponent() {
        // TODO: insert component initialization logic here
    }

    @Override
    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    @Override
    @NotNull
    public String getComponentName() {
        return "RunCmdComponent";
    }

    public void commandsBatProccess(AnActionEvent anActionEvent, String path, String filePrex, String... commands) {

        String filePath = null;
        filePath = GameUtils.createBatFileWithPathLimited(anActionEvent, path, filePrex, commands);

        runFile(filePath);
    }
    // start file, for example, run .bat file
    public void runFile(String path) {
        String cmd0 = "cmd /c start " + path;
        runCmd(cmd0);
    }

    public void runCmd(String... commands) {
        Runtime rt = Runtime.getRuntime(); //Runtime.getRuntime()返回当前应用程序的Runtime对象
        Process ps = null;  //Process可以控制该子进程的执行或获取该子进程的信息。
        try {
            for(String command : commands) {
                ps = rt.exec(command);   //该对象的
                // exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序，并返回与该子进程对应的Process对象实例。
                ps.waitFor();  //等待子进程完成再往下执行。
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    // 移动文件，先删除将移至的文件夹，再将本身文件夹移动后删除
    public void moveAndCopyFile(AnActionEvent anActionEvent, String batFileNamePrex, String oldFilePath, String newFilePath) {

        String filePath;
        String command1 = "rd /s/q " + newFilePath;
        String command2 = "md " + newFilePath;
        String command3 = "xcopy " + oldFilePath + " " + newFilePath + "/s /e /y";
        String command4 = "rd /s/q " + oldFilePath;

        filePath = GameUtils.createBatFile(anActionEvent, batFileNamePrex, command1, command2, command3, command4);
        runFile(filePath);

    }
    // 完全复制，会先删除将要移动到的文件夹，本身文件夹保留
    public void copyFile(AnActionEvent anActionEvent, String batFileNamePrex, String oldFilePath, String newFilePath) {

        String filePath;
        String command1 = "rd /s/q " + newFilePath;
        String command2 = "md " + newFilePath;
        String command3 = "xcopy " + oldFilePath + " " + newFilePath + "/s /e /y";

        filePath = GameUtils.createBatFile(anActionEvent, batFileNamePrex, command1, command2, command3);
        runFile(filePath);

    }

    // 不复制子目录及其下文件夹，将要移至的文件夹保持不变，本身文件夹保留,
    public void moveFile(AnActionEvent anActionEvent, String batFileNamePrex, String oldFilePath, String newFilePath) {

        String filePath;
        String command1 = "copy " + oldFilePath + " " + newFilePath;

        filePath = GameUtils.createBatFile(anActionEvent, batFileNamePrex, command1);
        runFile(filePath);

    }

    // 移动文件，原文件夹不变
    public void moveFiles(AnActionEvent anActionEvent, String batFileNamePrex, List<List<String>> oldToNewFilePath) {

        List<String> commands = new ArrayList<>();
        for (List<String> filePathPair : oldToNewFilePath) {
            String oldFilePath = filePathPair.get(0);
            String newFilePath = filePathPair.get(1);
            commands.add( "cd " + oldFilePath );
//            commands.add( "del /F /S /Q " + newFilePath );
//            commands.add( "md " + newFilePath );
            commands.add(  "xcopy " + oldFilePath + " " + newFilePath + "/s /e /y");
            commands.add(  "del /F /S /Q " + oldFilePath);
        }

        String[] strs = new String[commands.size()];

        String filePath;
        filePath = GameUtils.createBatFile(anActionEvent, batFileNamePrex,  commands.toArray(strs));
        runFile(filePath);

    }
}